package pers.cz.client;

import io.netty.channel.ChannelFuture;
import pers.cz.config.JefConfiguration;
import pers.cz.netty.client.TcpNettyClient;
import pers.cz.netty.client.UdpNettyClient;
import pers.cz.netty.config.NettyConfigItem;
import pers.cz.netty.protocal.Message;
import pers.cz.netty.protocal.MsgType;

/**
 * @program: JefLogTail
 * @description: 初始化netty客户端
 * @author: Cheng Zhi
 * @create: 2023-03-13 20:33
 **/
public class MessageClient {

    static {
        // 初始化客户端
        String host = JefConfiguration.get(NettyConfigItem.NETTY_SERVER_IP);
        int port = JefConfiguration.getInt(NettyConfigItem.NETTY_SERVER_PORT, 4567);

        if ("UDP".equalsIgnoreCase(JefConfiguration.get(NettyConfigItem.NETTY_TYPE, "TCP"))) {
            new UdpNettyClient(host, port).connect();
        } else {
            new TcpNettyClient(host, port).connect();
        }
    }

    public static void sendMessage(String msg) {

        Message message = new Message();
        message.setMsgType(MsgType.DATA);
        message.setContent(msg);


        if ("UDP".equalsIgnoreCase(JefConfiguration.get(NettyConfigItem.NETTY_TYPE, "TCP"))) {
            UdpNettyClient.sendMsg(message);
        } else {
            TcpNettyClient.sendMsg(message);
        }
    }
}
